package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.CaptureImage;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.YsToken;
import edu.ynu.se.xiecheng.achitectureclass.project.service.YsCaptureService;
import edu.ynu.se.xiecheng.achitectureclass.project.service.YsTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ys")
@CrossOrigin
@Slf4j
@Api(tags = "萤石数据接口")
public class YsTokenController {

    @Autowired
    private YsTokenService ysTokenService;

    @Autowired
    private YsCaptureService ysCaptureService;

    @GetMapping("/token")
    @ApiOperation(value = "获取萤石云token")
    public ResponseEntity<YsToken> getToken() {
        try {
            YsToken token = ysTokenService.getToken();
            return ResponseEntity.ok(token);
        } catch (Exception e) {
            log.error("Failed to get token", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

//    @PostMapping("/capture")
//    @ApiOperation(value = "抓拍")
//    public ResponseEntity<Map<String, String>> capture(@RequestBody Map<String, String> request) {
//        try {
//            String imageUrl = ysCaptureService.capture(request.get("accessToken"));
//            Map<String, String> response = new HashMap<>();
//            response.put("code", "200");
//            response.put("picUrl", imageUrl);
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            log.error("Failed to capture", e);
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
//        }
//    }

    @PostMapping("/capture")
    @ApiOperation(value = "抓拍")
    public ResponseEntity<Map<String, Object>> capture(@RequestBody Map<String, String> request) {
        try {
            Map<String, Object> result = ysCaptureService.capture(request.get("accessToken"));
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("抓拍失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/capture/history")
    @ApiOperation(value = "获取历史抓拍图片")
    public ResponseEntity<?> getHistoryImages(@RequestParam(defaultValue = "4") int limit) {
        try {
            List<CaptureImage> images = ysCaptureService.getRecentImages(limit);
            return ResponseEntity.ok(images);
        } catch (Exception e) {
            log.error("Failed to get history images", e);
            Map<String, String> error = new HashMap<>();
            error.put("message", "获取历史图片失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(error);
        }
    }


}